package com.ukao.pad.dialog;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.ukao.pad.R;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.widget.StateImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.bingoogolapple.qrcode.core.DisplayUtils;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;



public class QuiltSweepDialogFragment extends BaseDialogFragment {


    @BindView(R.id.tv_title)
    TextView title;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.txt_shouldpice)
    TextView txtShouldpice;
    @BindView(R.id.cdCode)
    ImageView cdCode;
    @BindView(R.id.txt_hite)
    TextView txtHite;

    private String codeUrl;
    private int shouldPice;
    private Unbinder unbinder;
    boolean isWeChat;



    public static QuiltSweepDialogFragment newInstance(String codeUrl, int shouldPice,boolean  isWeChat) {
        QuiltSweepDialogFragment fragment = new QuiltSweepDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, codeUrl);
        args.putInt(ARG_PARAM2, shouldPice);
        args.putBoolean(ARG_PARAM3, isWeChat);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        codeUrl = getArguments().getString(ARG_PARAM1);
        shouldPice = getArguments().getInt(ARG_PARAM2,0);
        isWeChat = getArguments().getBoolean(ARG_PARAM3,false);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_quilt_sweep_fragment, container, false);
        unbinder= ButterKnife.bind(this, view);
        getDialog().setCanceledOnTouchOutside(false);  //点击不消失

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        super.initView();
        title.setText(isWeChat ? " 微信支付" : "支付宝支付");
        txtShouldpice.setText("¥ " + CheckUtils.isEmptyNumber(shouldPice));
        createQRCode(codeUrl);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void createQRCode(String code) {

        QRCodeEncoder.encodeQRCode(code, DisplayUtils.dp2px(activity, 330), new QRCodeEncoder.Delegate() {

            @Override
            public void onEncodeQRCodeSuccess(Bitmap bitmap) {
                cdCode.setImageBitmap(bitmap);
            }


            @Override
            public void onEncodeQRCodeFailure() {
                T.show("生成二维码失败");
            }
        });
    }




    @OnClick({R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
            /*解锁*/
        if (mOnConfirmClickListener!=null){
            mOnConfirmClickListener.onChildItemClick(0,"");
        }
        mOnConfirmClickListener=null;
    }
}
